package com.pospi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.pospi.dto.GoodsDto;
import com.pospi.fragment.BlankFragment;
import com.pospi.modle.GoodsBeen;
import com.pospi.pai.yunpos.R;
import com.pospi.util.constant.URL;

import java.util.List;

/**
 * Created by acer on 2018/8/17.
 */

public class SearchGoodAdapter extends BaseAdapter {
    //上下文对象
    private Context context;
    private List<GoodsDto> goodsBeens;
    //无图设置
    private boolean wutu;
    public SearchGoodAdapter(Context context, List<GoodsDto> goodsBeens ,boolean mode) {
        this.context = context;
        this.goodsBeens = goodsBeens;
        wutu = mode;
    }

    @Override
    public int getCount() {
        return goodsBeens.size();
    }

    @Override
    public Object getItem(int item) {
        return item;
    }

    @Override
    public long getItemId(int id) {
        return id;
    }

    //创建View方法
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.gv_goods_item, null);
            holder.good_image = (ImageView) convertView.findViewById(R.id.img_good);
            holder.tv_good_name = (TextView) convertView.findViewById(R.id.tv_goodName);
            holder.tv_code = (TextView) convertView.findViewById(R.id.tv_code);
            holder.tv_name = convertView.findViewById(R.id.tv_name);
            holder.tv_goodPrice = convertView.findViewById(R.id.tv_goodPrice);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (!wutu) {
            Glide.with(context).load(new URL().host() + goodsBeens.get(position)
                    .getImage()).placeholder(R.drawable.image_none).dontAnimate().into(holder.good_image);
            holder.good_image.setVisibility(View.VISIBLE);
            holder.tv_name.setVisibility(View.GONE);
        } else {
            holder.good_image.setVisibility(View.GONE);
            holder.tv_name.setVisibility(View.VISIBLE);
            holder.tv_name.setText(goodsBeens.get(position).getName());
        }
        holder.tv_code.setText(goodsBeens.get(position).getCode());
        holder.tv_good_name.setText(goodsBeens.get(position).getName());
        holder.tv_goodPrice.setText(String.format("￥%s",
                String.valueOf(goodsBeens.get(position).getPrice())) + "/" + goodsBeens.get(position).getUnit());
        return convertView;
    }

    class ViewHolder {
        ImageView good_image;
        TextView tv_good_name;
        TextView tv_goodPrice;
        TextView tv_name;
        TextView tv_code;
    }
}
